﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Startup.Base.Resources
{
	public class AssemblyResourceReader
	{
		private readonly Assembly _assembly;

		public AssemblyResourceReader(Assembly assembly)
		{
			_assembly = assembly;
		}

		public string ReadAllTextFrom(string path)
		{
			if (string.IsNullOrEmpty(path))
				throw new ArgumentNullException("path");

			Stream targetStream = _assembly.GetManifestResourceStream(path);

			try
			{
				if (targetStream == null)
					throw new ArgumentException(string.Format("There is no resource with following name: {0}", path));

				using (var reader = new StreamReader(targetStream))
				{
					return reader.ReadToEnd();
				}
			}
			finally
			{
				if (targetStream != null)
					targetStream.Dispose();
			}
		}
		public string[] ReadAllLinesFrom(string path)
		{
			if (string.IsNullOrEmpty(path))
				throw new ArgumentNullException("path");

			Stream targetStream = _assembly.GetManifestResourceStream(path);

			try
			{
				if (targetStream == null)
					throw new ArgumentException(string.Format("There is no resource with following name: {0}", path));

				using (var reader = new StreamReader(targetStream))
				{
					List<string> lines = new List<string>();
					while (!reader.EndOfStream)
					{
						lines.Add(reader.ReadLine());
					}

					return lines.ToArray();
				}
			}
			finally
			{
				if (targetStream != null)
					targetStream.Dispose();
			}
		}
	}
}
